package com.platform.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLEncoder;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.HashMap;

import java.util.Map;

public class CloudDemo {

    public static void main(String[] args) {

        CloudDemo cloudDemo = new CloudDemo();

        Map<String, String> params = new HashMap<String, String>();
        String str = MD5Utils.encode("UsZ16798BUhrgjCREI" + "bc258ae368594b4084fd1fcac52d60dd").toUpperCase();
        params.put("secret_key", "UsZ16798BUhrgjCREI");
        params.put("secret_code", "c3e50f627ff6452ba44688ba8883f2dc");
        params.put("secret_sign", str);
        params.put("param", "{\"com\":\"zhongtong\",\"num\":\"75391502435530\",\"from\":\"\",\"phone\":\"\",\"to\":\"\",\"resultv2\":0,\"show\":\"0\",\"order\":\"desc\"}");
        System.out.println(cloudDemo.post(params));

    }

    public String post(Map<String, String> params) {

        StringBuilder response = new StringBuilder("");

        BufferedReader reader = null;

        try {

            StringBuilder builder = new StringBuilder();

            for (Map.Entry<String, String> param : params.entrySet()) {

                if (builder.length() > 0) {

                    builder.append('&');

                }

                builder.append(URLEncoder.encode(param.getKey(), "UTF-8"));

                builder.append('=');

                builder.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));

            }

            byte[] bytes = builder.toString().getBytes("UTF-8");

            URL url = new URL("http://cloud.kuaidi100.com/api");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setConnectTimeout(3000);

            conn.setReadTimeout(3000);

            conn.setRequestMethod("POST");

            conn.setRequestProperty("accept", "*/*");

            conn.setRequestProperty("connection", "Keep-Alive");

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));

            conn.setDoOutput(true);

            conn.getOutputStream().write(bytes);

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            String line = "";

            while ((line = reader.readLine()) != null) {

                response.append(line);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (null != reader) {

                    reader.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return response.toString();

    }

}

class MD5Utils {

    private static MessageDigest mdigest = null;

    private static char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    private static MessageDigest getMdInst() {

        if (null == mdigest) {

            try {

                mdigest = MessageDigest.getInstance("MD5");

            } catch (NoSuchAlgorithmException e) {

                e.printStackTrace();

            }

        }

        return mdigest;

    }

    public static String encode(String s) {

        if (null == s) {

            return "";

        }

        try {

            byte[] bytes = s.getBytes();

            getMdInst().update(bytes);

            byte[] md = getMdInst().digest();

            int j = md.length;

            char str[] = new char[j * 2];

            int k = 0;

            for (int i = 0; i < j; i++) {

                byte byte0 = md[i];

                str[k++] = digits[byte0 >>> 4 & 0xf];

                str[k++] = digits[byte0 & 0xf];

            }

            return new String(str);

        } catch (Exception e) {

            e.printStackTrace();

            return null;

        }

    }

}